package com.jiangnan.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter4.BaseQuickAdapter
import com.jiangnan.R
import com.jiangnan.databinding.ItemRecommendationBinding
import com.jiangnan.entity.BookRecommendationDTO

//class RecommendationAdapter(private var items: List<BookRecommendationDTO>)
//    : RecyclerView.Adapter<RecommendationAdapter.ViewHolder>() {
//
//    fun updateData(newItems: List<BookRecommendationDTO>) {
//        items = newItems
//        notifyDataSetChanged()
//    }
//
//    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
//        val tvTitle: TextView = itemView.findViewById(R.id.tvTitle)
//        val tvAuthor: TextView = itemView.findViewById(R.id.tvAuthor)
//        val tvRating: TextView = itemView.findViewById(R.id.tvRating)
//        val ivCover: ImageView = itemView.findViewById(R.id.ivCover)
//    }
//
//    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
//        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_recommendation, parent, false)
//        return ViewHolder(view)
//    }
//
//    override fun getItemCount(): Int = items.size
//
//    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//        val recommendation = items[position]
//        holder.tvTitle.text = recommendation.book.title
//        holder.tvAuthor.text = recommendation.book.author
//        holder.tvRating.text = "预估评分: ${String.format("%.2f", recommendation.predictedRating)}"
//        // 使用 Glide 加载图书封面
//        if (!recommendation.book.img.isNullOrEmpty()) {
//            Glide.with(holder.itemView.context)
//                .load(recommendation.book.img)
//                .into(holder.ivCover)
//        } else {
//            holder.ivCover.setImageResource(R.drawable.img_default) // 请添加一个占位图
//        }
//    }
//}


class RecommendationAdapter(

) : BaseQuickAdapter<BookRecommendationDTO, RecommendationAdapter.VH>() {

    // 自定义ViewHolder类
    class VH(
        parent: ViewGroup,
        val binding: ItemRecommendationBinding = ItemRecommendationBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        // 返回一个 ViewHolder
        return VH(parent)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: BookRecommendationDTO?) {
        // 设置item数据
        item ?.let {
            holder.binding.tvTitle.text = it.book.title
            holder.binding.tvAuthor.text = it.book.author
            holder.binding.tvRating.text = "预估评分: ${String.format("%.2f", it.predictedRating)}"
            // 使用 Glide 加载图书封面
            if (!it.book.img.isNullOrEmpty()) {
                Glide.with(holder.itemView.context)
                    .load(it.book.img)
                   .into(holder.binding.ivCover)
            }
         }
    }

}